﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace FHOClient
{
    public class HUDTextBoxObject
    {
        private Texture2D myPic;
        public int X;
        public int Y;

        public string TextData;
        public string displayTextData;
        public bool IsPassword;
        public bool IsSelected;
        private KeyboardState oldKeyboardState;
        

        public void Setup(GraphicsDeviceManager graphics, string FileName, Color TransparencyColor)
        {
            TextureCreationParameters tcp = TextureCreationParameters.Default;
            tcp.ColorKey = TransparencyColor;
            tcp.Format = SurfaceFormat.Rgba32;
            myPic = Texture2D.FromFile(graphics.GraphicsDevice, FileName, tcp);
        }

        public Texture2D Picture
        {
            get { return myPic; }
        }

        public int Width
        {
            get { return myPic.Width; }
        }

        public int Height
        {
            get { return myPic.Height; }
        }

        public void UpdateText(KeyboardState currentKeyboardState)
        {
            Keys[] pressedKeys;
            bool IsShifted;
            pressedKeys = currentKeyboardState.GetPressedKeys();
            if ((currentKeyboardState.IsKeyDown(Keys.RightShift)) || (currentKeyboardState.IsKeyDown(Keys.LeftShift)))
            {
                IsShifted = true;
            }
            else
            {
                IsShifted = false;
            }
            foreach (Keys key in pressedKeys)
            {
                if (oldKeyboardState.IsKeyUp(key))
                {
                    if (key == Keys.Back) // overflows
                    {
                        if (TextData.Length > 0)
                        {
                            TextData = TextData.Remove(TextData.Length - 1, 1);
                        }
                    }
                    else if ((key == Keys.LeftShift) || 
                            (key == Keys.RightShift) || 
                            (key == Keys.Tab) ||
                            (key == Keys.CapsLock) ||
                            (key == Keys.LeftControl) ||
                            (key == Keys.LeftAlt) ||
                            (key == Keys.RightControl) ||
                            (key == Keys.RightAlt)
                            )
                    {
                    }
                    else
                    {
                        if (key == Keys.Space)
                            TextData = TextData.Insert(TextData.Length, " ");
                        else
                        {
                            if (IsShifted)
                            {
                                switch (key)
                                {
                                    case Keys.D1:
                                        TextData += "!";
                                        break;
                                    case Keys.D2:
                                        TextData += "@";
                                        break;
                                    case Keys.D3:
                                        TextData += "#";
                                        break;
                                    case Keys.D4:
                                        TextData += "$";
                                        break;
                                    case Keys.D5:
                                        TextData += "%";
                                        break;
                                    case Keys.D6:
                                        TextData += "^";
                                        break;
                                    case Keys.D7:
                                        TextData += "&";
                                        break;
                                    case Keys.D8:
                                        TextData += "*";
                                        break;
                                    case Keys.D9:
                                        TextData += "(";
                                        break;
                                    case Keys.D0:
                                        TextData += ")";
                                        break;
                                    case Keys.OemMinus:
                                        TextData += "_";
                                        break;
                                    case Keys.OemPlus:
                                        TextData += "+";
                                        break;
                                    case Keys.OemTilde:
                                        TextData += "~";
                                        break;
                                    default:
                                        TextData += key.ToString();
                                        break;
                                }

                                
                            }
                            else
                            {
                                switch (key)
                                {
                                    case Keys.D1:
                                        TextData += "1";
                                        break;
                                    case Keys.D2:
                                        TextData += "2";
                                        break;
                                    case Keys.D3:
                                        TextData += "3";
                                        break;
                                    case Keys.D4:
                                        TextData += "4";
                                        break;
                                    case Keys.D5:
                                        TextData += "5";
                                        break;
                                    case Keys.D6:
                                        TextData += "6";
                                        break;
                                    case Keys.D7:
                                        TextData += "7";
                                        break;
                                    case Keys.D8:
                                        TextData += "8";
                                        break;
                                    case Keys.D9:
                                        TextData += "9";
                                        break;
                                    case Keys.D0:
                                        TextData += "0";
                                        break;
                                    case Keys.OemMinus:
                                        TextData += "-";
                                        break;
                                    case Keys.OemPlus:
                                        TextData += "=";
                                        break;
                                    case Keys.OemTilde:
                                        TextData += "`";
                                        break;
                                    default:
                                        TextData += key.ToString().ToLower();
                                        break;
                                        }
                            }
                        }
                    }
                    if (IsPassword == false)
                    {
                        displayTextData = TextData;
                    }
                    else
                    {
                        displayTextData = "";
                        for (int i=0; i< TextData.Length; i++)
                        {
                            displayTextData += "*";
                        }
                    }

                }
            }
            oldKeyboardState = currentKeyboardState;
        }
        
    }
}
